package com.markyao.aop;

import com.markyao.annotation.RegistKey;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Set;

@Slf4j(topic = "m.RecosRegistAspect")
@Component
@Aspect
public class RecosRegistAspect {

    @Autowired
    Set<String>serverKeySet;


    @Pointcut("within(com.markyao.recos.regist.RegistWorker)")
    public void pj(){}

    @Around("pj()")
    public Object afterRegist(ProceedingJoinPoint pjp) throws Throwable {
//        log.info("beforeRegist.....");
        Object result = pjp.proceed();

        MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
        Method method = methodSignature.getMethod();
        Annotation[][] parameterAnnotations = method.getParameterAnnotations();
        Object[] args = pjp.getArgs();

        for (int i = 0; i < args.length; i++) {
            Annotation[] annotations = parameterAnnotations[i];
            for (Annotation annotation : annotations) {
                if (annotation instanceof RegistKey) {
                    serverKeySet.add(args[i].toString());
                    log.info("regist key.....");
                }
            }
        }
        return result;
    }


}
